Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
eslint-plugin-react
Advanced tools
The eslint-plugin-react is a plugin for ESLint that provides linting utilities for React and JSX specific code. It helps developers adhere to best practices and avoid common pitfalls in React development.
JSX-specific rules
Enforces rules specific to JSX, such as ensuring that any variables used in JSX are defined and that React is in scope when using JSX.
"react/jsx-uses-react": "error", "react/jsx-uses-vars": "error"
Hooks rules
Provides rules that enforce the correct usage of React Hooks, such as ensuring that hooks are called in the same order on every render and that dependencies are specified correctly for useEffect.
"react-hooks/rules-of-hooks": "error", "react-hooks/exhaustive-deps": "warn"
Prop-types validation
Ensures that prop types are correctly defined and used within React components, helping to catch type-related bugs.
"react/prop-types": "error"
Component lifecycle
Warns about usage of deprecated methods in React component lifecycle, encouraging developers to update to newer APIs.
"react/no-deprecated": "warn"
Best practices
Encourages best practices by warning against common anti-patterns like using array indices as keys or mutating state directly.
"react/no-array-index-key": "warn", "react/no-direct-mutation-state": "error"
Similar to eslint-plugin-react, eslint-plugin-vue provides linting rules specific to Vue.js framework. It helps enforce Vue-specific best practices and coding standards.
This plugin is similar to eslint-plugin-react but for Angular applications. It contains rules that enforce best practices and conventions in AngularJS projects.
While not specific to React, eslint-plugin-jsx-a11y works well with eslint-plugin-react to enforce accessibility practices in JSX elements.
This plugin is designed for Preact (a fast 3kB alternative to React with the same ES6 API) and provides linting rules tailored to Preact codebases.
React specific linting rules for ESLint
Install ESLint either locally or globally.
$ npm install eslint
If you installed ESLint
globally, you have to install React plugin globally too. Otherwise, install it locally.
$ npm install eslint-plugin-react
Add plugins
section and specify ESLint-plugin-React as a plugin.
{
"plugins": [
"react"
]
}
You can also specify some settings that will be shared across all the plugin rules.
{
"settings": {
"react": {
"pragma": "React", // Pragma to use, default to "React"
"version": "0.14.0" // React version, default to the latest React stable release
}
}
}
If it is not already the case you must also configure ESLint
to support JSX.
With ESLint 1.x.x:
{
"ecmaFeatures": {
"jsx": true
}
}
With ESLint 2.x.x:
{
"parserOptions": {
"ecmaFeatures": {
"jsx": true
}
}
}
Finally, enable all of the rules that you would like to use.
The plugin has a recommended configuration that enforces React good practices.
displayName
in a React component definitionsetState
in componentDidMount
setState
in componentDidUpdate
this.state
isMounted
setState
ref
attribute.React
when using JSX.bind()
and arrow functions in JSX propsIf you're searching for React Native specific linting rules, check out eslint-plugin-react-native.
This plugin exports a recommended
configuration that enforce React good practices.
To enable this configuration use the extends
property in your .eslintrc
config file:
{
"plugins": [
"react"
],
"extends": ["eslint:recommended", "plugin:react/recommended"]
}
See ESLint documentation for more information about extending configuration files.
The rules enabled in this configuration are:
allow-in-func
optionallow-in-func
optionNote: This configuration will also enable JSX in parser options.
ESLint-plugin-React is licensed under the MIT License.
[4.3.0] - 2016-04-07
require-render-return
][] rule ([#482][] @shmuga)jsx-equals-spacing
][] ([#506][] @peet)jsx-closing-bracket-location
][] ([#511][] @KevinGrandon)prefer-stateless-function
][] for conditional JSX ([#516][])jsx-pascal-case
][] to support single letter component names ([#505][] @dthielman)FAQs
React specific linting rules for ESLint
The npm package eslint-plugin-react receives a total of 15,993,488 weekly downloads. As such, eslint-plugin-react popularity was classified as popular.
We found that eslint-plugin-react demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.